#pragma once

#include "Containers/Delegate.h"
#include "Framework/ApplicationSubsystem.h"
#include "DockWidgetSubsystem.m.h"

class RWidget;
class RDockLayout;
class RWindow;
class RDockableWindow;

//manage the dock widgets
RCLASS()
class WHENGINE_API RDockWidgetSubsystem : public RApplicationSubsystem
{
    ROBJECT

public:
    virtual void Initialize() override;
    virtual void Deinitialize() override;

    static RDockWidgetSubsystem* Get();

    //register a dock widget content, can be any widget
    //@InWidgetCreator  define how to create the widget instance
    //@UniqueWidgetID is used to identify the widget,unique in group
    void RegisterDockWidget(
        const CString & LayoutGroupName,
        const CString & UniqueWidgetID,
        const CString & Title,
        TDelegate<TSharedObjectPtr<RWidget>,RWidget* /*Parent widget*/ > InWidgetCreator
        );

    TSharedObjectPtr<RWidget> CreateDockWidget(const CString & LayoutGroupName,const CString & UniqueWidgetName, TSharedObjectPtr<RWidget> InParentWidget);
    CString GetDockWidgetTitle(const CString & LayoutGroupName,const CString & UniqueWidgetName);

    //find the dock layout under mouse cursor
    //return nullptr if not found
    TSharedObjectPtr<RDockLayout> FindDockLayoutUnderMouse(
        const CString& InLayoutGroupName
        ,TSharedObjectPtr<RWindow> ExcludeWindow
    );

    //save the all dock layout into string
    //@RootWidgetOfMainWindow is the root layout of main window
    //assume there is a main window in group, is parent window of other dockable windows
    CString SaveDockLayouts(TSharedObjectPtr<RDockLayout> RootWidgetOfMainWindow);

    //load the dock layout group from string
    TSharedObjectPtr<RDockLayout> LoadDockLayoutGroup(
        TSharedObjectPtr<RWidget> InParentWidget //the parent widget of the root layout of main window
        ,const CString& InLayoutGroupString //the string saved by SaveDockLayoutGroup
    );

protected:


    struct SDockWidgetCreateInfo
    {
        //unique in group
        CString UniqueWidgetID; 
        CString Title;
        TDelegate<TSharedObjectPtr<RWidget>,RWidget* /*Parent widget*/ > WidgetCreator;
        CString LayoutGroupName;
    };

    //all dock widgets
    TMap< CString/*Group Name*/,
        TMap<CString /*Widget ID*/ , 
            SDockWidgetCreateInfo>> DockWidgetCreators;


};